package com.gxa.meiye.admin.platform.controller;

import com.gxa.meiye.admin.platform.filter.OAuth2Token;
import com.gxa.meiye.admin.platform.pojo.domain.JwtToken;
import com.gxa.meiye.admin.platform.pojo.vo.AdminVo;
import com.gxa.meiye.admin.platform.pojo.vo.ListVo;
import com.gxa.meiye.admin.platform.pojo.vo.MenuVo;
import com.gxa.meiye.admin.platform.service.AdminService;
import com.gxa.meiye.admin.platform.utils.JwtUtil;
import com.gxa.meiye.admin.platform.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author bill
 * @date 2022/7/4 10:25
 */
@Api
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("登录方法")
    @PostMapping("/login")
    public Map login(@RequestBody AdminVo admin) throws Exception {
        if(admin.getAccount()!=null&&admin.getPwd()!=null) {
            Integer id = adminService.queryAdmin(admin);
            if(id!=null){
                String token = JwtUtil.sign(admin.getAccount(), admin.getPwd());
                Subject subject = SecurityUtils.getSubject();//拿到subject
                subject.login(new OAuth2Token(token));
                AdminVo adminVo = adminService.queryAdminById(id);
                System.out.println(token);
                //根据用户id返回List<MenuVo>(菜单列表)  将父级菜单放入ListVo的menuVo中，子级菜单放入menuVo中
//                List<MenuVo> menuVos = adminService.queryMenus(id);
                //查找父级菜单
                List<MenuVo> menuVos = adminService.selPMenu(id);
                List<ListVo> listVos = new ArrayList<>();
                for(MenuVo menuVo : menuVos){
                    List<MenuVo> menuVos1 = adminService.selSMenu(menuVo.getId());
                    System.out.println(menuVos1);
                    if(menuVos1!=null) {
                        ListVo listVo = new ListVo();
                        listVo.setMenuVo(menuVo);
                        listVo.setMenuVoList(menuVos1);
                        listVos.add(listVo);
                    }
                }
                System.out.println(listVos);
                return R.ok("登录成功").put("data",listVos).put("Authorization",token).put("user",adminVo);
            }
        }
        return R.error("登录失败");
    }

    @ApiOperation("修改密码方法")
    @PostMapping("/updatePwd")
    public Map updatePwd(@RequestBody AdminVo admin){

        //修改自定义盐值，使原token失效
        Integer integer = adminService.updatePwd(admin);
        if(integer!=0) {
            return R.ok("修改成功");
        }
        else {
            return R.error("修改失败");
        }
    }
}
